//
//  YMStackView.swift
//  YMSwiftUI
//
//  Created by anscen on 2022/3/10.
//
import UIKit
import SwiftUI
import Moya
//
struct YMStackView :View {
    @State var isAlert  = false
    @State var listData :[YMStack] = []
    var body: some View{
        GeometryReader { root in
            VStack(alignment: .center, spacing: 0.0) {
                List(listData,id: \.self) { model in
                    HStack {
                        Text(model.title).font(Font(UIFont.systemFont(ofSize: 20, weight: .medium))).foregroundColor(Color.appx000000()).padding(EdgeInsets(top: 10, leading: 0, bottom: 10, trailing: 0))
    //                    Spacer()
                        NavigationLink {
                            YMStackMoreView(id: model.id)
                        } label: {
                            //Image("icon_more").frame(width: 44, height: 44, alignment: .center)
                        }
                    }
                    ForEach(model.listData, id: \.self){
                        item in
                        YMListCell(book: item,width: root.size.width)
                    }
                }.onAppear {
                    self.loadData()
                    debugPrint("root.size.width")
                    debugPrint(root.size.width)
                }.listStyle(.plain)
            }
        }
    }
    func loadData(){
        var listDatas :[YMStack] = []
        let list = ["5a6844f8fc84c2b8efaa8bc5","54d42d92321052167dfb75e3","5a6844aafc84c2b8efaa6b6e"]
        let group = DispatchGroup()
        list.forEach { model in
            group.enter()
            ApiMoya.request(target: .homeHot(model)) { json  in

                let rank = json["ranking"]
                let data = [YMBook].deserialize(from: rank["books"].rawString())
                if let list = data as? [YMBook]{
                    let count = list.count > 8 ? 8 : list.count;
                    let data = [] + list.prefix(count)
                    let objc = YMStack(title: rank["title"].stringValue, id: model, listData: data)
                    listDatas.append(objc)
                }
                group.leave()
            } failure: { error in
                group.leave()
            }
        }
        group.notify(queue: DispatchQueue.main) {
            self.listData = listDatas
        }
    }
}
//
//struct YMStackMoreView :View{
//    @State var title :String
//    var body :some View{
//        HStack {
//            Text(title).font(Font(UIFont.systemFont(ofSize: 18, weight: .medium))).foregroundColor(Color.appx000000())
//            Spacer()
//            NavigationLink.init {
//                
//            } label: {
//                Text("更多").font(Font.system(size: 14)).foregroundColor(Color.appx999999())
//            }
//        }.padding(EdgeInsets.init(top: 12, leading: 15, bottom: 12, trailing: 15))
//    }
//}
//
//struct YMStackDetailView :View{
//    var body: some View{
//        VStack(alignment: .center, spacing: 30) {
//            Text("儿子视图")
//            YYStackGridView()
//            YYStackListView()
//        }.background(.blue)
//    }
//}
//struct YYStackGridView :View{
//    @EnvironmentObject var client :YYClient
//    var body: some View{
//
//        VStack.init(alignment: .center, spacing: 30) {
//            Text("孙子网格")
//            Text("昵称 : \(client.nickName)").foregroundColor(.white)
//            Text("电话 : \(client.phone)").foregroundColor(.white)
//            
//            Button.init {
//                loadData()
//            } label: {
//                Text("改变").frame(width: 100, height: 45, alignment: .center).foregroundColor(.white)
//            }.background(AppColor)
//        }.background(.yellow)
//    }
//    func loadData(){
//        client.nickName = "战神吕布"
//        debugPrint(client.nickName)
//    }
//}
//struct YYStackListView :View{
//    @EnvironmentObject var client :YYClient
//    var body: some View{
//        
//        VStack.init(alignment: .center, spacing: 30) {
//            Text("孙子表格")
//            Text("昵称 : \(client.nickName)").foregroundColor(.white)
//            Text("电话 : \(client.phone)").foregroundColor(.white)
//            
//            Button.init {
//                loadData()
//            } label: {
//                Text("改变").frame(width: 100, height: 45, alignment: .center).foregroundColor(.white)
//            }.background(AppColor)
//        }.background(.red)
//    }
//    func loadData(){
//        client.nickName = "诸葛亮"
//        debugPrint(client.nickName)
//    }
//}
//
//
//class YYClient :ObservableObject{
//    @Published var nickName :String = ""
//    @Published var phone    :String = ""
////    init(nickName :String,phone :String) {
////        self.nickName = nickName
////        self.phone = phone
////    }
//}
